home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #2 / Monster Media No. 2 (Monster Media)(1994).ISO / prog_gen / gcoope10.zip / LOWSTRM.C < prev    next >
Text File  |  1994-07-21  |  4KB  |  176 lines

  1. /*
  2.  
  3.     LowStream class definition for GCOOPE 1.0
  4.  
  5.     Compatible with experimental strong typing option.
  6.  
  7.     Designed 7/21/94 by Brian L. Price
  8.  
  9.     Released as Public Domain    July, 1994.
  10.  
  11.         This is NOT a standalone class, it must be inherited by a
  12.     class that also inherits the Dynmem class.
  13.  
  14.     NOTE: This class definition is an extension class, while it
  15.     provides no functionality on its own, it extends the capabilities
  16.     of another class.
  17.         The purpose of this class is to provide low level stream i/o
  18.     compatibility to any inheritor of the Dynmem class.  In other
  19.     words, this class can be used to allow arrays and strings to be
  20.     targets of a stream i/o operation.
  21.         The technique illustrated here is roughly analogous to a C++
  22.     friend concept but does its job with complete instance and class
  23.     encapsulation.  The only presumption made by this class is that
  24.     an inheritor also inherits the Dynmem class.  (Or has inherited
  25.     a class which has inherited Dynmem.)
  26.         Another feature shown here is the local storage of the
  27.     instance handle adjusted for a call to Dynmem, thus avoiding a
  28.     lengthy recursive call to steer with a fake instance every time a
  29.     call to a Dynmem method is made.  This technique could be made even
  30.     faster by storing the method function addresses thus avoiding the
  31.     dispatcher overhead.  (At the cost of storing two method function
  32.     pointers (8 bytes).)
  33.  
  34. */
  35.  
  36.  
  37. #define CLASS LowStream
  38.  
  39. #include "gcoope10.h"
  40. #include <stdio.h>
  41.  
  42. object CLASS;
  43.  
  44. extern object Dynmem;
  45.  
  46. typedef struct {
  47.     object    dynhandle;
  48.     word    strmPos;
  49.     stat     strmErr;
  50.         } instVars;
  51.  
  52. USEGEN(addressOf);
  53. USEGEN(sizeOf);
  54.  
  55. USEGEN(getPos);
  56. USEGEN(setPos);
  57. USEGEN(putByte);
  58. USEGEN(getByte);
  59. USEGEN(strmErr);
  60. USEGEN(clrErr);
  61.  
  62.  
  63. cmethod object m4New(object instance)
  64. {
  65. instVars *      ivptr;
  66. object        fake=0;
  67.  
  68. if(NULL==(ivptr=makeInst(&instance))) return 0;
  69. ivptr->strmPos=0;
  70. ivptr->strmErr=FUNCOKAY;
  71. (tag) fake = (tag) instance;
  72. ivptr->dynhandle=steer(Dynmem, fake);
  73.  
  74. return instance;
  75. }
  76.  
  77.  
  78. imethod long m4getPos(object instance)
  79. {
  80. return ((instVars *) getIVptr(instance))->strmPos;
  81. }
  82.  
  83.  
  84. imethod object m4strmErr(object instance)
  85. {
  86. return ((instVars *) getIVptr(instance))->strmErr;
  87. }
  88.  
  89.  
  90. imethod object m4setPos(object instance, long offset, int start)
  91. {
  92. instVars *     ivptr;
  93. word        size;
  94.  
  95. if(NULL==(ivptr=getIVptr(instance))) goto err;
  96. size=((WRDRV)g)(GEN(sizeOf))(ivptr->dynhandle);
  97. size--;
  98.  
  99. switch (start) {
  100.     case SEEK_SET : ivptr->strmPos = 0;
  101.     case SEEK_CUR : break;
  102.     case SEEK_END : ivptr->strmPos= size; break;
  103.     default    : goto err; }
  104.  
  105. offset+=ivptr->strmPos;
  106. if(offset >= 0 && offset <= size)
  107.     {
  108.     ivptr->strmPos=(word) offset;
  109.     if(offset==0) ivptr->strmErr=FUNCOKAY;
  110.     return FUNCOKAY;
  111.     }
  112.  
  113. err:
  114. return FUNCFAIL;
  115. }
  116.  
  117.  
  118. imethod object m4putByte(object instance, byte newData)
  119. {
  120. instVars *     ivptr;
  121. byte *        bptr;
  122.  
  123. if(NULL==(ivptr=getIVptr(instance)) || ivptr->strmErr) goto err;
  124. bptr=((byte *)((VDPTRRV) g)(GEN(addressOf))(ivptr->dynhandle))
  125.     + ivptr->strmPos++;
  126. *bptr=newData;
  127. if(ivptr->strmPos >= ((WRDRV) g)(GEN(sizeOf))(ivptr->dynhandle))
  128.     ivptr->strmErr=FUNCFAIL;
  129. return FUNCOKAY;
  130.  
  131. err:
  132. return FUNCFAIL;
  133. }
  134.  
  135.  
  136. imethod int m4getByte(object instance)
  137. {
  138. instVars *    ivptr;
  139. byte *        bptr;
  140.  
  141. if(NULL==(ivptr=getIVptr(instance)) || ivptr->strmErr) goto err;
  142. bptr=((byte *)((VDPTRRV) g)(GEN(addressOf))(ivptr->dynhandle))
  143.     + ivptr->strmPos++;
  144. if(ivptr->strmPos >= ((WRDRV) g)(GEN(sizeOf))(ivptr->dynhandle))
  145.     ivptr->strmErr=FUNCFAIL;
  146. return *bptr;
  147.  
  148. err:
  149. return -1;
  150. }
  151.  
  152.  
  153. imethod object m4clrErr(object instance)
  154. {
  155. instVars *     ivptr;
  156.  
  157. if(NULL==(ivptr=getIVptr(instance))) return FUNCFAIL;
  158. else
  159.     {
  160.     ivptr->strmErr=FUNCOKAY;
  161.     return FUNCOKAY;
  162.     }
  163. }
  164.  
  165.  
  166.  
  167. CLASS_INSTALL
  168. {
  169. if(END==(CLASS=g(New)(Class, 0, sizeof(instVars), END))
  170.     || addGMthd(CLASS,New,(method) m4New) || ADDGM(getPos)
  171.     || ADDGM(strmErr) || ADDGM(setPos) || ADDGM(putByte)
  172.     || ADDGM(getByte) || ADDGM(clrErr)) return FUNCFAIL;
  173. else return FUNCOKAY;
  174. }
  175.  
  176.